package com.satan.fmmall.controller;

import com.satan.fmmall.entity.Orders;
import com.satan.fmmall.service.OrderService;
import com.satan.fmmall.vo.ResCode;
import com.satan.fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * @ClassName OrderController
 * @Description TODO
 * @date 2022/4/17 16:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理", value = "提供订单相关操作的接口")
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping("/add")
    @ApiOperation("订单添加接口")
    public ResultVo addOrder(@RequestBody Orders orders, String cartIds) {
        System.out.println(orders);
        System.out.println(cartIds);
        ResultVo resultVo = null;
        try {
            resultVo = orderService.addOrder(cartIds, orders);
        } catch (SQLException e) {
            resultVo = ResultVo.fail(ResCode.ORDER_ERROR);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return resultVo;

    }

    @ApiOperation("查询订单状态接口")
    @GetMapping("/status/{orderId}")
    public ResultVo getOrderStatus(@PathVariable("orderId") String orderId) throws Exception {
        return ResultVo.success(orderService.getOrderById(orderId));
    }

    @ApiOperation("订单查询接口")
    @GetMapping("/list")
    public ResultVo list(String userId, String status, Integer pageNum, Integer pageSize) {
        System.out.println("userId = " + userId);
        System.out.println("status = " + status);
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        return orderService.listOrders(userId, status, pageNum, pageSize);
    }
    @ApiOperation("删除订单接口")
    @DeleteMapping("/delete/{orderId}")
    public ResultVo deleteOrder(@PathVariable("orderId") String orderId) {
        ResultVo resultVo = null;
        try {
            orderService.deleteOrderByOrderId(orderId);
            resultVo=ResultVo.success(null);
        }catch (Exception e){
            resultVo=ResultVo.fail(ResCode.DELETE_ERROR);
        }
        return resultVo;
    }


}
